import pyodbc

# 定义数据库连接信息
connection_dict = {
    'server': 'localhost',
    'database': 'xhedu',
    'username': 'xhedu',
    'password': 'xhedu'
}


def execute_sql_query(query):
    # 创建连接字符串
    conn_str = (
        "DRIVER={SQL Server};"
        f"SERVER={connection_dict['server']};"
        f"DATABASE={connection_dict['database']};"
        f"UID={connection_dict['username']};"
        f"PWD={connection_dict['password']};"
    )

    # 连接数据库
    with pyodbc.connect(conn_str) as conn:
        # 创建游标
        with conn.cursor() as cursor:
            # 执行查询语句
            cursor.execute(query)

            # 返回查询结果
            return cursor.fetchall()


# 定义查询语句
query = "select top 10 * from edu_lesson"

# 执行查询并处理结果
results = execute_sql_query(query)

# 输出结果
for row in results:
    print(row)
